package com.ecp.commons.storage;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import com.ecp.commons.Environment;
import com.ecp.commons.Global;
import com.ecp.commons.filter.WebContextFactory;

/** * @author  wangyin: 
    * @date    2015年11月11日 下午4:13:05
    */
public class StorageService extends Storage implements IStorageService {
	private String				dir;
	public static final String	imgPath		= "http://img.pinzhi365.com";
	private static final String	rootPath	= "/home/data";

	public static void main(String[] args) {
		String url = StorageService.imgPath + "/ad/dsdsd/asdsa.jsp";
		System.out.println(getRealPathByUrl(url));
	}

	static public String getRealPathByUrl(String url) {
		if (url == null) {
			url = "";
		}
		//如果传入的url是包含图片域名的就把图片域名去掉
		if (url.startsWith(StorageService.imgPath)) {
			url = url.substring(StorageService.imgPath.length());
		}
		String rUrl = "";
		if (Environment.isWindows()) {
			String webAppPath = Environment.getEnv().getWebAppPath();
			rUrl = webAppPath
					+ (url.length() <= 0 ? "" : url.substring(1).replace("/", File.separator));
		} else {
			//			String webAppPath = Environment.getEnv().getWebAppPath();
			//			rUrl = webAppPath + url;
			rUrl = rootPath + url;
		}
		return rUrl;
	}

	public StorageService(String dir) {
		this.dir = dir;
	}

	public String setPicPath(String relativePath) {
		String serverRealPath = WebContextFactory.getWebContext().getRequest().getSession()
				.getServletContext().getRealPath(this.getBasePath() + relativePath);
		if (Environment.isWindows()) {
			serverRealPath = WebContextFactory.getWebContext().getRequest().getSession()
					.getServletContext().getRealPath(this.getBasePath() + relativePath);
		} else {
			serverRealPath = rootPath + this.getBasePath() + relativePath;
		}
		return serverRealPath;
	}

	@Override
	public String getBasePath() {
		return this.dir;
	}

	@Override
	public String getShowBasePath() {
		return imgPath + this.dir;
	}

	@Override
	public File readFile(String path) {
		String realPath = "";
		//		if (Environment.isWindows()) {
		//			realPath = WebContextFactory.getWebContext().getRealPath() + getBasePath() + path;
		//		} else {
		//			realPath = rootPath + path;
		//		}
		realPath = WebContextFactory.getWebContext().getRealPath() + getBasePath() + path;
		return new File(realPath);
	}

	@Override
	public void delete(String path) {
		if (StringUtils.isBlank(path)) {
			return;
		}
		String realPath = "";
		//		if (Environment.isWindows()) {
		//			realPath = WebContextFactory.getWebContext().getRealPath() + getBasePath() + path;
		//		} else {
		//			realPath = rootPath + path;
		//		}
		//		realPath = WebContextFactory.getWebContext().getRealPath() + getBasePath() + path;
		realPath = this.setPicPath(path);
		File dest = new File(realPath);
		FileUtils.deleteQuietly(dest);
	}

	@Override
	public String absolutePath() {
		String realPath = Global.SERVER_ADDR + getBasePath();
		return realPath;
	}
}
